/**
 * @ author: 汪孟苹
 */
define(["jquery","template","scprit/followup/template/comm/comm.tpl.js"],function($,template,tpl){
	var comm = {};
	
	/**
	 * 模态框
	 * 	className 模态框class名
	 *  tit		  模态框title
	 *  id        模态框modal-body的id名
	 *  size      模态框size  modal-lg  modal-sm  默认 正常的
	 *  showModalFooter	是否显示模态框底部        默认false
	 *  footerId		模态框底部id
	 */
	var drawcreateModal = function(data){
		return template("tpl_comm_createModal",data);
	}
	comm.createModal = function(obj,callback){
		var default_modal = {
			"showModalFooter":false		// 默认不显示模态框尾部
		};
		obj = $.extend(default_modal,obj);
		$("#content-main").append(tpl.createModalInit());	// content-main为右侧大盒子的id
		var h = drawcreateModal(obj);
		$("#content-main").append(h);
		if(callback && typeof(callback) === "function"){
			callback();
		}
	};
	/**
	 * 模态框
	 *  id        模态框外包裹的box "modalBox"
	 * 	className 模态框class名
	 * 	size      模态框size  modal-lg  modal-sm  默认 正常的
	 *  showModalFooter	是否显示模态框底部        默认false
	 *  tit		  模态框title
	 */
	var drawModal = function(data){
		return template("tpl_comm_modal",data);
	}
	comm.modal = function(options,callback){
		var data = {
			"id":"modalBox",
			"className":"myModal",
			"showModalFooter":false		// 默认不显示模态框尾部
		};
		$.extend(data,options);
		$("#" + data.id).html(tpl.modalInit());
		var h = drawModal(data);
		$("#" + data.id).html(h);
		if(callback && typeof(callback) === "function"){
			callback();
		}
	};
	/**
	 * div
	 */
	var drawcreateDiv = function(data){
		return template("tpl_comm_createDiv",data);
	}
	comm.createDiv = function(options){
		options.dom.html(tpl.createDivInit());
		var html = drawcreateDiv(options);
		options.dom.html(html);
	};
	/**
	 * 随机数
	 */
	comm.random = function(){
		return Math.floor(Math.random()*100000000);
	}
	/**
	 * 格式化年龄
	 * 入参 总天数 days
	 * 1、小于三个月的显示几周几天，(days<3*30)
	 * 2、小于1岁的显示几月龄几天，	(3*30<=days<1*365)
	 * 3、大于一岁的显示几岁几月龄，(1*365<=days<6*365)
	 * 4、大于六岁的只显示岁。		(days>=6*365)
	 */
	var formatAge = function(days){
		var age = "";
		if(days < 3*30){
			var week = parseInt(days / 7);
			var day = parseInt(days % 7);
			if(week != 0) age += week + "周";
			age += day + "天";
		}else if(days < 1*365){
			var month = parseInt(days / 30);
			var day = parseInt(days % 30);
			if(month != 0) age += month + "月龄";
			if(day != 0) age += day + "天";
		}else if(days < 6*365){
			var year = parseInt(days / 365);
			var month = parseInt(days % 365 / 30);
			if(year != 0) age += year + "岁";
			if(month != 0) age += month + "月龄";
		}else{
			var year = parseInt(days / 365);
			if(year != 0) age += year + "岁";
		}
		return age;
	}
	comm.formatAge = function(days){
		return formatAge(days);
	}
	/**
	 * 两个日期之间的差
	 */
	var dateDiff = function(date1,date2){
		var date1 = new Date(date1);
		var date2 = new Date(date2);
		var s1 = date1.getTime();
		var s2 = date2.getTime();
		var total = (s2 - s1)/1000;
		var days = parseInt(total / (24*60*60)); // 两日期之间的差（天数）
		return {
			days:days,
			age:formatAge(days)
		};
	}
	comm.dateDiff = function(date1,date2){
		return dateDiff(date1,date2);
	}
	/**
	 * 计算孕周
	 */
	comm.getGestation = function(date1,date2){
		var day = dateDiff(date1,date2).days;
		var allDayActual = 40*7-day;
		var weekActual = parseInt(allDayActual / 7);
		var dayActual = parseInt(allDayActual % 7);
		return {weekActual:weekActual,dayActual:dayActual};
	}
	/**
	 * 获取时间（时间格式：xxxx-xx-xx）,根据日期对象
	 */
	var fillZero = function(s){
	    return s < 10 ? '0' + s: s;
	}
	var getDateFormat = function(date){
		var year = date.getFullYear();
		var month = date.getMonth()+1;
		var days = date.getDate(); 
		var hours = date.getHours();
		var minutes = date.getMinutes();
		var seconds = date.getSeconds();
		var dates = year +'-'+ fillZero(month) +"-"+ fillZero(days);
		var locale = dates + ' ' + fillZero(hours) + ":" + fillZero(minutes) + ":" + fillZero(seconds);
		return {
			"date":dates,
			"locale":locale
		};
	}
	comm.getDateFormat = function(date){
		return getDateFormat(date);
	}
	/**
	 * 获取当前时间
	 */
	comm.getCurTime = function(){
		var myDate = new Date();
		return getDateFormat(myDate);
	}
	/**
	 * 获取数字保留小数的位数
	 */
	comm.getDecimalPlaces = function(value){
		var n; //保留了n位小数
		var index = value.toString().indexOf(".");
        if (index > 0){
        	var length = value.toString().length;
        	n = length-index-1;
        }else{
        	n = 0;
        }
        return n;
	}
	/**
	 * select选择框里的option 
	 * selectQueue
	 */
	var drawselectQueue = function(data){
		return template("tpl_comm_selectQueue",data);
	}
	comm.selectQueue = function(options){
		var data = {
			rows: options.list
		}
		$("#"+options.id).html(tpl.selectQueueInit());
		var h = drawselectQueue(data);
		$("#"+options.id).html(h);
	}

	return comm;
});